package site.marqstree.latte.ec.main.sort.content;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.entity.node.BaseNode;

import java.util.List;

import butterknife.BindView;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.profile.AppProfile;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.sort
 * 文件名: ContentDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/29 13:01
 * 描述: 右侧明细
 */
public class ContentDelegate extends LatteDelegate {

    private static final String ARG_CONTENT_ID = "CONTENT_ID";
    @BindView(R2.id.rv_list_content)
    RecyclerView mRecyclerView;
    private NodeSectionAdapter mAdapter;
    private int mContentId = -1;
    private List<BaseNode> mData = null;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mContentId = args.getInt(ARG_CONTENT_ID);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_list_content;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData();
        initRecyclerView();
    }

    //创建实例
    public static ContentDelegate newInstance(int contentId) {
        final Bundle args = new Bundle();
        args.putInt(ARG_CONTENT_ID, contentId);
        final ContentDelegate delegate = new ContentDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    //获取数据
    private void initData() {
        String url = AppProfile.SORT_CONTENT_DATA_URL.replace("CONTENTID", mContentId+"");
        RestClient.builder()
                .url(url)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        mData = new SectionDataConverter().convert(response);
                        mAdapter.setNewData(mData);
                        mRecyclerView.scheduleLayoutAnimation();
                    }
                })
                .build()
                .get();
    }

    //初始化RecyclerView
    private void initRecyclerView() {
        //设置3列宽
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mAdapter = new NodeSectionAdapter();

        //// 顶部header
        //View view = getLayoutInflater().inflate(R.layout.section_head_sort_content, mRecyclerView, false);
        //mAdapter.addHeaderView(view);

        //绑定适配器
        mRecyclerView.setAdapter(mAdapter);
    }
}
